package com.ielts.fragment.adapter;

import java.util.ArrayList;

import vn.com.baseapplication.util.Utils;
import android.content.Context;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.ielts.activity.R;
import com.ielts.model.ItemTopics;

public class TopicAdapter extends BaseAdapter {
	Context context;
	ArrayList<ItemTopics> listData;

	public TopicAdapter(Context context, ArrayList<ItemTopics> listItem) {
		this.context = context;
		this.listData = listItem;
	}

	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public ItemTopics getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.layout_item_ielts, null);
			IeltsItemHolder holder = new IeltsItemHolder(convertView);
			convertView.setTag(holder);
		}
		IeltsItemHolder holder = (IeltsItemHolder) convertView.getTag();
		holder.setData(listData.get(position));
		return convertView;
	}

	class IeltsItemHolder {
		TextView month, date, content;
		ImageView more;

		public IeltsItemHolder(View parent) {
			month = (TextView) parent.findViewById(R.id.item_month);
			date = (TextView) parent.findViewById(R.id.item_date);
			content = (TextView) parent.findViewById(R.id.item_content);
			more = (ImageView) parent.findViewById(R.id.item_more);
		}

		public void setData(ItemTopics data) {
			data.refreshData();
			String examDate = data.examDate;
			String monthStr = Utils.getMonthStrFromNumber(Integer.parseInt(examDate.split("-")[1]));
			String dateStr = examDate.split("-")[2];
			month.setText(monthStr);
			date.setText(dateStr);
			content.setText(data.topic);
			
			// more.setCompoundDrawables(null,draw , null, null);
			int icon = data.getFreeResIcon();
			if(data.topicType.equalsIgnoreCase("F")){
				icon = R.drawable.ic_free;
			}
			else if(data.topicType.equalsIgnoreCase("P")){
				icon = -1;
			}
			else if(data.topicType.equalsIgnoreCase("B")){
				icon = R.drawable.ic_sticket_bought;
			}
//			more.setCompoundDrawablesWithIntrinsicBounds(0, icon > 0 ? icon : 0, 0, 0);
			if(icon>0){
				more.setImageResource(icon);
				more.setVisibility(View.VISIBLE);
			}else{
				more.setVisibility(View.INVISIBLE);
			}
			more.setTag(data.id);
		}

	}

}
